package cn.talkimages.web.admin.actions.knowledge;

import cn.talkimages.framework.utils.string.StringUtil;
import cn.talkimages.web.admin.constant.AdminConstDef;
import cn.talkimages.web.admin.constant.MessageKeys;
import cn.talkimages.web.beans.AdminBean;
import cn.talkimages.web.beans.KnowledgeBean;

public class KnowledgeUpdateAction extends KnowledgeAction {

	private static final long serialVersionUID = 1239381650044320038L;

	private String updateType;

	// ----------------------- Form Properties ---------------------- //

	/** 编号 */
	private String id;

	/** 标题 */
	private String title;

	/** 分享内容 */
	private String content;

	// ---------------------------- 更新 ---------------------------- //

	/**
	 * 知识共享 新建/更新
	 * 
	 * @throws Exception
	 */
	public String update() throws Exception {

		String result = ERROR;

		if (StringUtil.isBlank(id)) {
			updateType = AdminConstDef.UpdateType.Add;

			result = add();

		} else {
			updateType = AdminConstDef.UpdateType.Edit;

			result = edit();
		}

		return result;
	}

	// ---------------------------- 创建 ---------------------------- //

	/**
	 * 编辑知识新建
	 * 
	 * @throws Exception
	 */
	public String add() throws Exception {

		return SUCCESS;
	}

	// ---------------------------- 编辑 ---------------------------- //

	/**
	 * 编辑知识共享
	 * 
	 * @throws Exception
	 */
	public String edit() throws Exception {

		return SUCCESS;
	}

	// ---------------------------- 提交 ---------------------------- //

	/**
	 * 保存知识分享
	 * 
	 * @throws Exception
	 */
	public String save() throws Exception {

		// 检查入力数据有效性
		if (!validateInputData()) {
			return INPUT;
		}

		// gather data from form
		KnowledgeBean knowledgeBean = packageData();

		if (AdminConstDef.UpdateType.Add.equals(updateType)) {
			knowledgeService.insertKnowledge(knowledgeBean);
		}

		return SUCCESS;
	}

	/**
	 * 检查入力数据有效性
	 * 
	 * @return boolean 有效:true 有错误数据:false
	 */
	protected boolean validateInputData() {

		if (StringUtil.isBlank(title)) {
			addActionError(getText(MessageKeys.Common.FIELD_REQUIRED,
					new String[] { "标题" }));
		} else if (title.length() > 50) {
			addActionError(getText(MessageKeys.Common.FIELD_MAX_LENGTH,
					new String[] { "50" }));
		}

		if (StringUtil.isBlank(content)) {
			addActionError(getText(MessageKeys.Common.FIELD_REQUIRED,
					new String[] { "分享内容" }));
		}

		return getActionErrors().size() == 0;
	}

	/**
	 * 收集表单数据
	 * 
	 * @return KnowledgeBean
	 */
	protected KnowledgeBean packageData() {

		AdminBean loginUser = getLoginUser();
		KnowledgeBean knowledge = new KnowledgeBean();

		knowledge.setTitle(title);
		knowledge.setContent(content);
		knowledge.setCreateId(loginUser.getLoginId());
		knowledge.setUpdateId(loginUser.getLoginId());

		return knowledge;
	}

	/**
	 * 填充表单数据
	 */
	protected void unpackageData(KnowledgeBean knowledge) {

		id = String.valueOf(knowledge.getId());
		title = knowledge.getTitle();
		content = knowledge.getContent();
	};

	// ---------------------------- 删除 ---------------------------- //

	/**
	 * 删除知识共享
	 * 
	 * @throws Exception
	 */
	protected String delete() throws Exception {

		updateType = AdminConstDef.UpdateType.Delete;

		return SUCCESS;
	}

	// ----------------------- Getter & Setter ----------------------- //

	public String getUpdateType() {
		return updateType;
	}

	public void setUpdateType(String updateType) {
		this.updateType = updateType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}
